#include <iostream>
#include <ctime>
#include "block_queue.hpp"
using namespace std;
#define pnum 1
#define cnum 1

std::string opers = "+-*/%";
void* productor(void* args)
{
    BlockQueue<Task>* bq = static_cast<BlockQueue<Task>*>(args);

    while(true)
    {
        int x = rand() % 10;
        int y = rand() % 10;
        char op = opers[rand() % opers.size()];
        Task t(x, y, op);
        bq->push(t);
        std::cout << t.put_task() << std::endl;
        // sleep(1);
    }
    return nullptr;

}
void* consumer(void* args)
{
    BlockQueue<Task>* bq = static_cast<BlockQueue<Task>*>(args);
    while(true)
    {
        Task t = bq->pop();
        t();
        std::cout << t.get_result() << std::endl;
    }
    return nullptr;
}
int main()
{
    srand(time(nullptr));
    BlockQueue<Task>* bq = new BlockQueue<Task>;
    pthread_t p[pnum], c[cnum];
    for(int i = 0; i < pnum; ++i)
    {
        pthread_create(p + i, nullptr, productor, bq);
    }
    for(int i = 0; i < cnum; ++i)
    {
        pthread_create(c + i, nullptr, consumer, bq);
    }
    
    
    for(int i = 0; i < pnum; ++i)
    {
        pthread_join(p[i], nullptr);
    }
    for(int i = 0; i < cnum; ++i)
    {
        pthread_join(c[i], nullptr);
    }
    delete bq;
    return 0;
}